package com.xiaozhou.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xiaozhou.function.FunctionManager;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.core.response.HttpxBinaryResponseContent;
import com.zhipu.oapi.service.v4.api.DocumentClientApiService;
import com.zhipu.oapi.service.v4.api.KnowledgeClientApiService;
import com.zhipu.oapi.service.v4.knowledge.*;
import com.zhipu.oapi.service.v4.knowledge.document.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author shhzhou
 * @description: 知识库 范例
 * @date 2024/11/4 17:46
 */
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {


    @Autowired
    private ClientV4 clientV4;

    @Autowired
    private FunctionManager functionManager;


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.KnowledgeResponse
     * @Author xiaozhou
     * @Description 创建知识库      id = 1853613069343805440
     * @Date 9:12 2024/11/5
     * @Param
     */
    @GetMapping("/createKnoledge")
    public KnowledgeResponse createKnoledge() {
        KnowledgeBaseParams params = KnowledgeBaseParams.builder()
                .embeddingId(3)
                .name("小周的知识库")
                .description("这是小周创建的知识库，用于对接接口测试使用。")
                .build();

        KnowledgeResponse apply = new KnowledgeClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).knowledgeCreate(params).apply(clientV4);
        return apply;

    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.KnowledgeEditResponse
     * @Author xiaozhou
     * @Description 修改知识库
     * @Date 9:19 2024/11/5
     * @Param
     */
    @GetMapping("/editKnoledge")
    public KnowledgeEditResponse editKnoledge() {
        KnowledgeBaseParams params = KnowledgeBaseParams.builder()
                .knowledgeId("1853606334231105536")
                .embeddingId(3)
                .name("小周的知识库-修改版")
                .description("这是小周创建的知识库，用于对接接口测试使用。")
                .build();

        KnowledgeEditResponse apply = new KnowledgeClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).knowledgeModify(params).apply(clientV4);
        return apply;

    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.QueryKnowledgeApiResponse
     * @Author xiaozhou
     * @Description 检索知识库列表
     * @Date 9:26 2024/11/5
     * @Param
     */
    @GetMapping("/queryKnoledge")
    public QueryKnowledgeApiResponse queryKnoledge() {
        QueryKnowledgeRequest params = QueryKnowledgeRequest.builder()
                .page(1)
                .size(10)
                .build();

        QueryKnowledgeApiResponse apply = new KnowledgeClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).knowledgeQuery(params).apply(clientV4);
        return apply;
    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.KnowledgeEditResponse
     * @Author xiaozhou
     * @Description 删除知识库
     * @Date 9:31 2024/11/5
     * @Param
     */
    @GetMapping("/deleteKnoledge")
    public KnowledgeEditResponse deleteKnoledge() {
        KnowledgeBaseParams params = KnowledgeBaseParams.builder()
                .knowledgeId("1853606334231105536")
                .build();

        KnowledgeEditResponse apply = new KnowledgeClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).knowledgeDelete(params).apply(clientV4);
        return apply;
    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.KnowledgeUsedResponse
     * @Author xiaozhou
     * @Description 知识库使用量查询
     * @Date 9:41 2024/11/5
     * @Param
     */
    @GetMapping("/usedKnoledge")
    public KnowledgeUsedResponse usedKnoledge() {
        KnowledgeUsedResponse apply = new KnowledgeClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).knowledgeUsed().apply(clientV4);
        return apply;
    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.document.DocumentObjectResponse
     * @Author xiaozhou
     * @Description 文件管理  上传文件     documentId  =   1853619732654194688
     * @Date 9:51 2024/11/5
     * @Param
     */
    @GetMapping("/createDocument")
    public DocumentObjectResponse createDocument() throws JsonProcessingException {
        DocumentCreateParams params = DocumentCreateParams.builder()
                .filePath("logs/测试文档.docx")
                .purpose("retrieval")
                .knowledgeId("1853613069343805440")
                .customSeparator(null)
                .sentenceSize(300)
                .build();

        DocumentObjectResponse apply = new DocumentClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).createDocument(params).apply(clientV4);
        return apply;
    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.document.DocumentEditResponse
     * @Author xiaozhou
     * @Description 文件管理  删除文件
     * @Date 10:02 2024/11/5
     * @Param
     */
    @GetMapping("/deleteDocument")
    public DocumentEditResponse deleteDocument() {
        DocumentEditParams params = DocumentEditParams.builder()
                .id("1853616858519961600")
                .build();

        DocumentEditResponse apply = new DocumentClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).deleteDocument(params).apply(clientV4);
        return apply;
    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.document.QueryDocumentApiResponse
     * @Author xiaozhou
     * @Description 文件管理  查询文件列表
     * @Date 10:06 2024/11/5
     * @Param
     */
    @GetMapping("/queryDocument")
    public QueryDocumentApiResponse queryDocument() {
        QueryDocumentRequest params = QueryDocumentRequest.builder()
                .purpose("retrieval")
                .knowledgeId("1853613069343805440")
                .page(1)
                .limit(10)
                .build();

        QueryDocumentApiResponse apply = new DocumentClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).queryDocumentList(params).apply(clientV4);
        return apply;
    }


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.document.DocumentDataResponse
     * @Author xiaozhou
     * @Description 文件管理   知识库文件详情
     * @Date 10:09 2024/11/5
     * @Param
     */
    @GetMapping("/infoDocument")
    public DocumentDataResponse infoDocument() {
        DocumentEditParams params = DocumentEditParams.builder()
                .id("1853619732654194688")
                .build();

        DocumentDataResponse apply = new DocumentClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).retrieveDocument(params).apply(clientV4);
        return apply;
    }


    /**
     * @Author xiaozhou
     * @Description 文件管理   编辑知识库文件
     * @Date 10:14 2024/11/5
     * @Param
     * @return
     * @return com.zhipu.oapi.service.v4.knowledge.document.DocumentEditResponse
     */
    @GetMapping("/editDocument")
    public DocumentEditResponse editDocument() {
        DocumentEditParams params = DocumentEditParams.builder()
                .id("1853619732654194688")
                .knowledgeType(1)
                .customSeparator(null)
                .sentenceSize(300)
                .build();

        DocumentEditResponse apply = new DocumentClientApiService(clientV4.getConfig().getHttpClient(), clientV4.getConfig().getBaseUrl()).modifyDocument(params).apply(clientV4);
        return apply;
    }


    // TODO 接口暂时不通，暂不清楚对接方式   报错：Failed to get the file content
    @GetMapping("/contentDocument")
    public void contentDocument() {
        try {
            HttpxBinaryResponseContent httpxBinaryResponseContent = clientV4.fileContent("1853635089657888768");
            String filePath = "demo_output";
            String resourcePath = KnowledgeController.class.getClassLoader().getResource("").getPath();
            httpxBinaryResponseContent.streamToFile(resourcePath+"1" +filePath,1000);
        }catch (IOException e){
            e.printStackTrace();
        }
    }





}
